// pages/common/pages/password/password.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldPassword:"",       //旧密码
    newPassword:"",       //新密码
    confirmPassword:"",   //确认新的密码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //页面加载完成
  },

  /**
   * 旧密码输入框，输入监听
   */
  inputOldPassword: function(e){
    this.setData({
      oldPassword: e.detail.value
    })
  },
  /**
   * 新密码输入框，输入监听
   */
  inputNewPassword: function(e){
    this.setData({
      newPassword: e.detail.value
    })
  },
  /**
   * 确认密码输入框，输入监听
   */
  confirmNewPassword: function(e){
    this.setData({
      confirmPassword: e.detail.value
    })
  },
  /**
   * 修改密码按钮事件监听
   */
  changePassword:function(){
    var md5 = require("../../../../utils/md5.js")
    let oldPwd = this.data.oldPassword;
    let newPwd = this.data.newPassword;
    let cnewPwd = this.data.confirmNewPassword;
    let _this = this;
    console.log("***", this.data.oldPassword + "&&" + this.data.newPassword + "&&" + this.data.confirmPassword)
    if (oldPwd == null || oldPwd==''){
      wx.showToast({
        title: "请输入旧密码",
        icon: "none"
      })
    } else if (newPwd == null || newPwd==''){
      wx.showToast({
        title: "请输入新密码",
        icon: "none"
      })
    } else if (cnewPwd==newPwd ){
      wx.showToast({
        title: "与新密码不符，请重新输入",
        icon: "none"
      })
    }else{
      wx.request({
        url: app.globalData.ipPort +'/front/auth/changeAdminPwd',
        method: 'post',
        header: {
          'Cookie': app.globalData.header.Cookie
        },
        data: {
          oldPassword: md5(oldPwd),
          newPassword: md5(newPwd)
        },
        success(res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: "密码已修改，请重新登录",
              icon: "none"
            })
            _this.reLogin();
            
          } else {
           
            wx.showToast({
              title: res.data.msg,
              icon: "none"
            })
          }
          _this.clearData();
        }, fail(res) {
          _this.clearData();
          wx.showToast({
            title: res.msg,
            icon: "none"
          })
        }
      })
    }
  },
  clearData(){
    this.setData({
      oldPassword: "",       //旧密码
      newPassword: "",       //新密码
      confirmPassword: "",   //确认新的密码
    })
  },
  /**
   * 关闭所有页面，重新登录
   */
  reLogin(){
    wx.reLaunch({
      url: '/pages/index/index'
    })
  }
})